package com.fitbet.wechat.web.controller.main;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.bet.UserBetSupport;
import com.fitbet.entity.user.User;
import com.fitbet.wechat.processor.WechatProcessor;
import com.fitbet.wechat.service.UserBetService;
import com.fitbet.wechat.service.UserService;
import com.fitbet.wechat.web.interceptor.UserContext;
import com.fitbet.wechat.web.interceptor.UserContextType;
import com.fitbet.wechat.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/users")
public class UserBetController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private UserBetService userBetService;
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    /**
     * 
     * @param userId
     * @param betId
     * @param request
     * @return
     */
    @RequestMapping(value = "/{userId}/bets/{betId}/share")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView userBetShare(@PathVariable("userId") Long userId,
                                     @PathVariable("betId") Long betId,
                                     HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("userbet/userbet-share");
        
        User user = userService.load(userId);
        modelAndView.addObject("user", user);
        
        UserBet userBet = userBetService.loadUserBet(userId, betId, new UserBetStatus[]{UserBetStatus.PROCESSING, UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED});
        modelAndView.addObject("userBet", userBet);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param userId
     * @param betId
     * @param request
     * @return
     */
    @RequestMapping(value = "/{userId}/bets/{betId}")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView userBetDetail(@Secure User user,
                                      @PathVariable("userId") Long userId,
                                      @PathVariable("betId") Long betId,
                                      HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("userbet/userbet-detail");
        
        modelAndView.addObject("user", user);
        
        UserBet userBet = userBetService.loadUserBet(userId, betId, new UserBetStatus[]{UserBetStatus.PROCESSING, UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED});
        modelAndView.addObject("userBet", userBet);
        
        UserBetSupport userBetSupport = userBetService.loadSupports(userBet, user);
        modelAndView.addObject("userBetSupport", userBetSupport);
        
        List<UserBetSupport> supports = userBetService.listSupports(userBet);
        modelAndView.addObject("supports", supports);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/{userId}/bets/{betId}/support")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView support(@Secure User user,
                                @PathVariable("userId") Long userId,
                                @PathVariable("betId") Long betId,
                                @RequestParam(name = "support", required = false, defaultValue = "true") boolean support,
                                HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("userbet/userbet-support");
        modelAndView.addObject("user", user);
        
        UserBet userBet = userBetService.loadUserBet(userId, betId, new UserBetStatus[]{UserBetStatus.PROCESSING, UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED});
        modelAndView.addObject("userBet", userBet);
        
        List<UserBetSupport> supports = userBetService.listSupports(userBet);
        modelAndView.addObject("supports", supports);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
}
